import { defineStore } from "pinia";
import {
  getPageListData,
  deletePageData,
  createPageData,
  editPageData,
} from "@/service/main/system/system";
const useSystemStore = defineStore("system", {
  state: () => ({
    pageContenList: [],
    pageListTotal: 0,
    offset: 0,
    size: 10
  }),
  actions: {
    async getPageListAction(pageName: string, queryInfo: any) {
      const pageUrl = `/${pageName}/list`;
      const data = await getPageListData(pageUrl, queryInfo);
      this.pageContenList = data.data.list;
      this.pageListTotal = data.data.totalCount;
    },

    async deleteListAction(pageName: string, id: number) {
      const pageUrl = `/${pageName}/${id}`;
      await deletePageData(pageUrl);
      this.getPageListAction(pageName, {
        offset: this.offset * this.size,
        size: this.size
      });
    },

    async createAction(pageName: string, userInfo: any) {
      await createPageData(pageName, userInfo);
      console.log("1");
      this.getPageListAction(pageName, {
        offset: 0,
        size: 10,
      });
    },

    async editAction(pageName: string, userInfo: any, userId: number) {
      // 1.编辑数据的请求
      const pageUrl = `/${pageName}/${userId}`;
      await editPageData(pageUrl, userInfo);
      console.log("1");

      // 2.请求最新的数据
      this.getPageListAction(pageName, {
        offset: this.offset * this.size,
        size: this.size
      });
    },
  },
});
export default useSystemStore;
